import connectMongoDb from "@/libs/mongodb";
import Topic from "@/models/topic";
import { NextResponse } from "next/server";

/**
 * POST
 * http://localhost:3000/api/topics
 * **/
export async function POST(request: any) {
    const { title, description } = await request.json();
    await connectMongoDb();

    await Topic.create({ title, description });
    return NextResponse.json({ message: "Topic Created" }, { status: 201 });
}

/**
 * GET
 * http://localhost:3000/api/topics
 * **/
export async function GET() {
    await connectMongoDb();
    const topics = await Topic.find();
    return NextResponse.json(topics);
}

/**
 * DELETE
 * http://localhost:3000/api/topics?id=123
 * **/
export async function DELETE(request: any) {
    const id = request.nextUrl.searchParams.get("id");
    await connectMongoDb();
    await Topic.findByIdAndDelete(id);
    return NextResponse.json({ message: "Topic Deleted" }, { status: 200 });
}